package com.sajus.billing.client.delegate;

import javax.naming.NamingException;

import com.sajus.billing.client.util.EJBLocalServiceLocator;
import com.sajus.billing.client.util.EJBServiceLocator;
import com.sajus.billing.client.util.EJBServiceLocator.EJBs;
import com.sajus.billing.client.util.ManagerProxy;


/**
 * Creates business delegates for the given EJBs
 * 
 * @author thomasli
 *
 */
public class BusinessDelegateFactory {  
	static private BusinessDelegateFactory instance = new BusinessDelegateFactory( EJBLocalServiceLocator.getInstance() );
	
	private EJBServiceLocator locator;
	
	private IServiceTypeManager serviceTypeManager;
	private IAccountManager accountManager;
	
	public void setLocator(EJBServiceLocator locator) {
		this.locator = locator;
	}

	static public BusinessDelegateFactory getInstance() {
		return instance; 
	}

	
	private BusinessDelegateFactory( EJBServiceLocator locator) {
		super();
		this.locator = locator;
	}
	

	public IServiceTypeManager getServiceTypeManager() throws NamingException {
		if ( serviceTypeManager != null ) {
			return serviceTypeManager;
		}
		IServiceTypeManager realObject = locator.locate( EJBs.ServiceTypeManagerBean );
		serviceTypeManager = ManagerProxy.newInstance( realObject, IServiceTypeManager.class );
		return serviceTypeManager;
		
	}
	
	public IAccountManager getAccountManager() throws NamingException {
		if ( accountManager != null ) {
			return accountManager;
		}
		IAccountManager realObject = locator.locate( EJBs.AccountManagerBean );
		accountManager = ManagerProxy.newInstance( realObject, IAccountManager.class );
		return accountManager;
		
	}

}
